package com.sp.app.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.sp.app.domain.Power;
import com.sp.app.domain.Role;
import com.sp.app.service.PowerService;
import com.sp.app.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.alibaba.fastjson.JSONObject;


@Controller
@RequestMapping("/Role")
public class RoleController {

	@Autowired
	private RoleService depotService;

	@Autowired
	private PowerService powerService;

	@RequestMapping("/update")
	public String update(String id,String rolename,String powerContent){
		Role role = new Role();
		role.setRoleid(id);
		role.setRolename(rolename);
		depotService.update(role);
		List<Role> list = depotService.QueryAll(role);
		Role role1 = list.get(0);
		Power power = new Power();
		if(powerContent.indexOf("110") != -1){
			powerContent = powerContent + ",1100";
		}
		if(powerContent.indexOf("120") != -1){
			powerContent = powerContent + ",1200";
		}
		if(powerContent.indexOf("130") != -1){
			powerContent = powerContent + ",1300";
		}
		if(powerContent.indexOf("140") != -1){
			powerContent = powerContent + ",1400";
		}
		if(powerContent.indexOf("150") != -1){
			powerContent = powerContent + ",1500";
		}
		power.setPower(powerContent);
		power.setRoleid(role1.getPowerid());
		powerService.update(power);
		return "redirect:/Role/queryAll";
	}

	@RequestMapping("/delete")
	public String delete(String ID){
		depotService.deleteByPrimaryKey(ID);
		return "redirect:/Role/queryAll";
	}

	@RequestMapping("/queryAll")
	public String queryAll(HttpServletRequest request,Role role){


		List<Role> list = depotService.QueryAll(role);
		request.setAttribute("list", list);

	   if(null != role.getRolename()){
			request.setAttribute("rolename", role.getRolename());
		}
		return "Role";
	}

	@RequestMapping("/add")
	public String  add(String roleid,String rolename,String powerContent){
		Power power1 = new Power();
		Role role = new Role();
		String powerid = String.valueOf(Math.random()).substring(2, 8);
		role.setRoleid(String.valueOf(Math.random()).substring(2, 6));
		role.setPowerid(powerid);
		role.setRolename(rolename);

		power1.setPower(powerContent);
		power1.setRoleid(powerid);
		power1.setId(String.valueOf(Math.random()).substring(2, 8));

		powerService.insert(power1);
		depotService.insert(role);
		return "redirect:/Role/queryAll";
	}

	@ResponseBody
	@RequestMapping("/queryOne")
	public String queryOne(String ID){
		JSONObject json = new JSONObject();
		Role depot = new Role();
		Power power = new Power();
		power.setId(ID);
		System.out.println(ID);
		depot.setRoleid(ID);

		List<Role> list = depotService.QueryAll(depot);

		Role role1 = list.get(0);
		Power power1 = powerService.selectByPrimaryKey(role1.getPowerid());
		String name = role1.getRolename();

		role1.setPowerid(power1.getPower());

		String data = json.toJSONString(role1);

		return data;
	}

	@ResponseBody
	@RequestMapping("/getAll")
	public String getAll(Role role){
		JSONObject json = new JSONObject();
		List<Role> list = depotService.QueryAll(role);

		String jsonq = json.toJSONString(list);

		System.out.println(jsonq);
		return jsonq;
	}


}
